package com.cms.fom.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.cms.common.utils.R;
import com.cms.fom.dao.MonitorDao;
import com.cms.fom.entity.MonitorEntity;
import com.cms.fom.feign.StuFeign;
import com.cms.fom.to.StudentTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.fom.dao.TaskDao;
import com.cms.fom.entity.TaskEntity;
import com.cms.fom.service.TaskService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("taskService")
public class TaskServiceImpl extends ServiceImpl<TaskDao, TaskEntity> implements TaskService {

    @Autowired
    private StuFeign stuFeign;

    @Resource
    private MonitorDao monitorDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<TaskEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("id", key).or().like("content", key);
        }
        wrapper.orderByDesc("create_time");
        IPage<TaskEntity> page = this.page(
                new Query<TaskEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveAll(TaskEntity task) {

        task.setIsShow(1);
        baseMapper.insert(task);
        R r = stuFeign.studentByClassAndNew();
        if(r.getCode() == 0){
            List<StudentTo> newStudents = r.getData("newStudents", new TypeReference<List<StudentTo>>() {
            });
            for (StudentTo student : newStudents) {
                MonitorEntity monitorEntity = new MonitorEntity();
                monitorEntity.setStudentId(student.getId());
                monitorEntity.setTaskId(task.getId());
                StringBuffer stringBuffer = new StringBuffer();
                LocalDate localDate = LocalDate.now();
                for (int i = 0; i < 14; i++) {
                    stringBuffer.append(localDate.minusDays(-i) + ",");
                }
                stringBuffer.substring(0, stringBuffer.length() - 2);
                monitorEntity.setTime(stringBuffer.toString());
                monitorDao.insert(monitorEntity);
            }
        }

        new Thread() {
            @Override
            public void run() {
                try {
                    Long id = task.getId();
//                    sleep(1000 * 60 * 60 * 24 * 14);
                    sleep(1000 * 60 * 20);
                    TaskEntity taskEntity = new TaskEntity();
                    taskEntity.setId(id);
                    taskEntity.setIsShow(0);
                    baseMapper.updateById(taskEntity);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        }.start();

    }

}